from rest_framework import serializers
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.views import APIView

from meiduo_admin.serializers.goods_visit_serializer import GoodsVisitSerializer
from orders.models import OrderInfo
from users.models import User
from datetime import date, timedelta
from goods.models import GoodsVisitCount
from meiduo_admin.utils import get_ip

class UseTotalCountView(APIView):
    # 指定权限
    permission_classes = [IsAdminUser]

    def get(self, request):
        '''统计用户总量'''
        count = User.objects.all().count()
        now_date = date.today()
        print('后台:'+get_ip(request))
        return Response(
            {
                "count": count,
                "date": now_date
            }
        )


class UseTodayTotalCountView(APIView):
    # 指定权限
    permission_classes = [IsAdminUser]

    def get(self, request):
        '''统计当天注册用户总量'''
        now_date = date.today()
        # 获取当日注册用户数量 date_joined 记录创建账户时间
        count = User.objects.filter(date_joined__gte=now_date).count()
        return Response(
            {
                "count": count,
                "date": now_date
            }
        )


class UseActiveTotalCountView(APIView):
    # 指定权限
    permission_classes = [IsAdminUser]

    def get(self, request):
        '''当天活跃用户量'''
        now_date = date.today()
        # 获取当日登录用户数量  last_login记录最后登录时间
        count = User.objects.filter(last_login__gte=now_date).count()
        return Response(
            {
                "count": count,
                "date": now_date
            }
        )


class UserOrderCountView(APIView):
    '''日下单用户'''
    # 指定管理员权限
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 获取当前日期
        now_date = date.today()
        # 获取当日下单用户数量  orders__create_time 订单创建时间
        # count = User.objects.filter(orders__create_time__gte=now_date).count()
        orders = OrderInfo.objects.filter(create_time__gte=now_date)
        count = orders.values('user_id').distinct().count()
        return Response({
            "count": count,
            "date": now_date
        })


class UserMonthCountView(APIView):
    '''月增用户'''
    # 指定权限
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 1、获取当前日期
        now_date = date.today()
        # 2、获取一个月前的日期
        start_date = now_date - timedelta(days=29)

        # 构建列表数据
        data_list = []
        for i in range(30):
            # 获取起始时间
            index_date = start_date + timedelta(days=i)
            # 获取第二天实现
            next_date = index_date + timedelta(days=1)
            count = User.objects.filter(date_joined__gte=index_date, date_joined__lt=next_date).count()
            data_list.append({'count': count, 'date': index_date})
        # 3、返回结果
        return Response(data_list)


class GoodsVisitView(APIView):
    '''商品类别访问量'''

    # 指定管理员权限
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 获取当天日期
        now_date = date.today()
        # 获取当天访问的商品分类数量信息
        data = GoodsVisitCount.objects.filter(date__gte=now_date)
        # 序列化返回分类数量
        ser = GoodsVisitSerializer(data, many=True)

        return Response(ser.data)
